package com.hongxun.budget.ui.fragment.sg;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hongxun.budget.R;
import com.hongxun.budget.ui.activity.sg.MiddleCheckAddActivity;
import com.hongxun.budget.ui.dialog.NoticeDialog;
import com.hongxun.budget.ui.fragment.BaseListFragment;
import com.hongxun.budget.utils.DateUtils;
import com.hongxun.budget.utils.LocationUtils;
import com.hongxun.budget.utils.RecyclerViewUtils;
import com.hongxun.budget.utils.SPUtils;
import com.hongxun.budget.utils.SgDBUtils;
import com.hongxun.budget.utils.SgStrKey;
import com.hongxun.budget.utils.StringUtils;
import com.hongxun.budget.utils.XUtils;
import com.hongxun.entity.sg.CYbgcCj;
import com.hongxun.greenentity.sg.CMiddleCheckEntity;
import com.hongxun.interfaces.ClickListener;
import com.hongxun.interfaces.NovaCallback;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.view.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 中间检查2级界面
 */
public class MiddleCheckList extends BaseListFragment {
    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;

    private String tabCode = "100";
    private double lat = 30.6270786740;
    private double lng = 104.1520643234;
    private SuperAdapter<CYbgcCj> superAdapter;
    private List<CYbgcCj> datas = new ArrayList<>();

    @Override
    protected int getMyListLayoutId() {
        return R.layout.sg_fragment_middlecheck_list;
    }

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return RecyclerViewUtils.getLinearLayoutManagerForVertical(context);
    }

    @Override
    public void initView() {
        super.initView();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.radioButton1:
                        tabCode = "100";
                        break;
                    case R.id.radioButton2:
                        tabCode = "200";
                        break;
                }
                onRefresh();
            }
        });
    }

    @Override
    public SuperAdapter getAdapter() {
        superAdapter = new SuperAdapter<CYbgcCj>(R.layout.sg_content_mc_detail_adapter) {

            @Override
            public void bindView(ViewHolder viewHolder, final CYbgcCj itemBean, int i) {
                try {
                    ImageView imageView = viewHolder.getView(R.id.iv1);
                    XUtils.loadImg(context, itemBean.getUrl(), imageView);
                    String aNull = StringUtils.getNull(itemBean.getBigType().equals("1") ? "杆塔" : "配电设备");
                    String bNull = StringUtils.getNull(itemBean.getNameOrCode());
                    ((TextView) viewHolder.getView(R.id.tv1)).setText(aNull + "：" + bNull);
                    ((TextView) viewHolder.getView(R.id.tv2)).setText(itemBean.getCountPitPole() + "");
                    ((TextView) viewHolder.getView(R.id.tv3)).setText(itemBean.getCountPitLx() + "");
                    ((TextView) viewHolder.getView(R.id.tv4)).setText(itemBean.getCountEarthed() + "");
                    ((TextView) viewHolder.getView(R.id.tv7)).setText(itemBean.getCountSbjc() + "");
                    ((TextView) viewHolder.getView(R.id.tv5)).setText(StringUtils.getNull(itemBean.getResult()));
                    ((TextView) viewHolder.getView(R.id.tv6)).setText(DateUtils.LongToStringyyMMdd_(itemBean.getTimeCheck()));
                } catch (NullPointerException e) {
                }
            }
        };
        superAdapter.setData(datas);
        superAdapter.setEmptyDataView(R.layout.view_empty);
        superAdapter.setOnItemClickListener(new SuperAdapter.OnItemClickListener<CYbgcCj>() {
            @Override
            public void onClick(int i, CYbgcCj o) {
                SPUtils.put(context, SgStrKey.uuidjl, o.getUuid());
                startActivity(new Intent(context, MiddleCheckAddActivity.class)
                        .putExtra("isNewAdd", false)
                        .putExtra("tabCode", o.getTabName()));
            }
        });
        superAdapter.setOnItemLongClickListener(new SuperAdapter.OnItemLongClickListener<CYbgcCj>() {
            @Override
            public void onLongClick(int i, CYbgcCj o) {
                longClickDel(o);
            }
        });
        return superAdapter;
    }

    //删除项目
    private void longClickDel(final CYbgcCj dhjl) {
        new NoticeDialog()
                .setTitle("删除记录")
                .setMsg(dhjl.getNameOrCode())
                .setCancelButton("取消", null)
                .setConfirmButton("删除", new ClickListener() {
                    @Override
                    public void click() {
                        datas.remove(dhjl);
                        superAdapter.notifyDataSetChanged();
                        SgDBUtils.deleteCMiddleCheckEntity(dhjl.getUuid());
                    }
                }).show(getFragmentManager(), "");
    }

    @Override
    public void onRefresh() {
        datas.clear();
        Observable.just(SgStrKey.uuidxm).subscribeOn(Schedulers.io()).map(new Func1<String, Boolean>() {
            @Override
            public Boolean call(String value) {
                String uuid = SPUtils.get(context, value, "") + "";
                List<CMiddleCheckEntity> list = SgDBUtils.getCMceList(uuid, tabCode);
                for (CMiddleCheckEntity entity : list) {
                    CYbgcCj ybgcjc = new CYbgcCj();
                    ybgcjc.setUrl(SgDBUtils.getYbgcUrl(entity.getGuid()));
                    ybgcjc.setUuid(entity.getGuid());
                    ybgcjc.setUuidSs(entity.getPGuid());
                    ybgcjc.setBigType(entity.getMCType());
                    ybgcjc.setNameOrCode(entity.getMCName());
                    ybgcjc.setCountPitPole(SgDBUtils.getCPolePitList(entity.getGuid()).size());
                    ybgcjc.setCountPitLx(SgDBUtils.getCLinePitList(entity.getGuid()).size());
                    ybgcjc.setCountEarthed(SgDBUtils.getCGroundList(entity.getGuid()).size());
                    ybgcjc.setCountSbjc(SgDBUtils.getCBasicsWorkList(entity.getGuid()).size());
                    ybgcjc.setResult(entity.getMCResult());
                    ybgcjc.setTimeCheck(entity.getMCTime().getTime());
                    ybgcjc.setTabName(entity.getMCMode());
                    ybgcjc.setLat(entity.getMCLatitude());
                    ybgcjc.setLng(entity.getMCLongitude());
                    datas.add(ybgcjc);
                }
                return true;
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean b) {
                superAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        onRefresh();
    }

    @OnClick(R.id.btn_add)
    public void onViewClicked() {
        new LocationUtils(context, context, 3, 0, new NovaCallback() {
            @Override
            public void success(int i, JSONObject obj) {
                lat = obj.getDoubleValue("latitude");
                lng = obj.getDoubleValue("longitude");
                addNewRecord();
            }

            @Override
            public void err(int i, int status, String err) {
                addNewRecord();
            }
        });
    }

    private void addNewRecord() {
        SPUtils.put(context, SgStrKey.uuidjl, "");//清空
        startActivity(new Intent(context, MiddleCheckAddActivity.class)
                .putExtra("isNewAdd", true)
                .putExtra("tabCode", tabCode)
                .putExtra("lat", lat)
                .putExtra("lng", lng));
    }

}
